<template>
  <div class="edit-page">
    <!-- 导航栏 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.push({ path: '/user' })"
    />
    <!-- 头像 -->
    <div class="portrait">
      <van-image
        fit="cover"
        round="cover"
        width="100"
        height="100"
        :src="userInfo.photo"
      />

      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 编辑栏 -->
    <van-cell
      title="名称"
      :value="userInfo.name"
      is-link
      @click="editNameClick"
    />
    <van-cell
      title="性别"
      :value="userInfo.gender ? '女' : '男'"
      is-link
      @click="showPopup = true"
    />
    <van-cell
      title="生日"
      :value="userInfo.birthday"
      is-link
      @click="showDate = true"
    />

    <!-- /编辑下面弹窗 -->
    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
      @confirm="updateName"
    >
      <van-field ref="input" v-model="newName" placeholder="请输入姓名" />
    </van-dialog>
    <!-- 修改性别弹出框 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-field label="男" is-link @click="updateGender(0)" />
      <van-field label="女" is-link @click="updateGender(1)" />
    </van-popup>
    <!-- 编辑生日弹窗 -->
    <van-popup v-model="showDate" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDate = false"
        @confirm="updateBirthday"
      />
    </van-popup>

    <!-- 图片裁剪 -->
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="updatephoto">确定</van-button>
    </div>
  </div>
</template>

<script>
//1 导入 mapState 辅助函数
import { mapState } from "vuex";

import * as moment from "moment";

//2 导入 编辑接口
import { editUserInfo, editUserortrait } from "./user.js";

//导入裁剪组件
import { VueCropper } from "vue-cropper";

export default {
  name: "edit",
  components: {
    VueCropper,
  },
  computed: {
    // 添加计算属性
    ...mapState(["userInfo"]),
  },
  data() {
    return {
      showDialog: false, //编辑姓名的弹窗
      newName: "", //姓名输入框
      showPopup: false, //编辑性别的弹窗
      showDate: false, //编辑时间的弹窗
      minDate: new Date(1900, 1, 1), //限制最大出生年龄
      maxDate: new Date(2030, 1, 1), //限制最小出生年龄
      currentDate: new Date(), //当前时间
      img: "", //要裁剪的图片
      showCropper: false, //裁剪框显示/隐藏
    };
  },

  methods: {
    editNameClick() {
      //1 弹出dialog 弹窗
      this.showDialog = true;
      //输入默认框是当前用户的名字
      this.newName = this.userInfo.name;
      //输入默认框成为焦点 （原生 dom.focus）

      setTimeout(() => {
        this.$refs.input.focus();
      }, 100);
    },
    //2 dialog 弹窗 comfirm 事件
    updateName() {
      //调用接口
     editUserInfo({ name: this.newName }).then((res) => {
        console.log(res);
        // 弹窗显示
        this.$toast.success("修改成功");
        //更新vuex里的数据
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          name: this.newName,
        });
      });
    },
    updateGender(gender) {
      editUserInfo({ gender }).then((res) => {
        // console.log(res);
        //1 提示修改成功
        this.$toast.success("修改成功");
        //2 关闭性别选项框
        this.showPopup = false;
        //更新vuex数据
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          gender,
        });
      });
    },
    //编辑生日
    updateBirthday() {
      //转换
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      // console.log(birthday);
      editUserInfo({ birthday }).then((res) => {
        // console.log(res);
        this.showDate = false;
        this.$toadt.success("修改成功");
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          birthday,
        });
      });
    }, //5 选择头像文件
    afterRead(file) {
      // console.log(file.file);
      // 5.1 sc formdata头像

      //1 设置裁剪图片的路径
      this.img = file.content;
      //2 显示裁剪盒子
      this.showCropper = true;

    },
    updatephoto(){
         //6 上传裁剪好的图片

      this.$refs.cropper.getCropBlob((data) => {
        let fd = new FormData();
        fd.append("photo", data);
        editUserortrait(fd).then((res) => {
          console.log(res);
          // 5.2 提示更新成功
          this.$toast.success("修改成功");
          // 5.3 隐藏裁剪框
          this.showCropper = false;
          //5.4 更新vuex
          this.$store.commit("updateUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
        });
      });

    }

  
  },
};
</script>

<style lang="less">
.edit-page {
  .van-hairline--bottom {
    background-color: #0098fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-nav-bar__text {
      color: #fff;
    }
    .van-icon {
      color: #fff;
    }
  }
  .portrait {
    height: 150px;
    display: flex;
    justify-content: center;
    align-items: center;
    .van-uploader {
      position: absolute;
      opacity: 0;
      width: 120px;
      height: 120px;
      .van-uploader {
        width: 120px;
        height: 120px;
      }
    }
  }
  .cropper {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 50%;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 50%;
    }
  }
}
</style>